package com.tfjybj.itoo.exam.provider.service.impl;

import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import com.dmsdbj.itoo.tool.base.service.impl.BaseServicePlusImpl;
import com.dmsdbj.itoo.tool.business.ItooResult;
import com.dmsdbj.itoo.tool.constantutil.YearSemester;
import com.tfjybj.itoo.exam.entity.*;
import com.tfjybj.itoo.exam.model.SelectTeacherTeachClassModel;
import com.tfjybj.itoo.exam.model.TeacherCourseTeachClassModel;
import com.tfjybj.itoo.exam.model.TemporaryClassStudentModel;
import com.tfjybj.itoo.exam.provider.dao.StudentDao;
import com.tfjybj.itoo.exam.provider.dao.TeacherCourseTeachClassDao;
import com.tfjybj.itoo.exam.provider.dao.TeacherTeachClassDao;
import com.tfjybj.itoo.exam.provider.service.StudentTeachClassService;
import com.tfjybj.itoo.exam.provider.service.TeachClassService;
import com.tfjybj.itoo.exam.provider.service.TeacherCourseTeachClassService;
import com.tfjybj.itoo.exam.provider.service.TeacherTeachClassService;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.*;
import java.util.concurrent.TimeUnit;

/**
 * TeacherCourseTeachClassService接口实现类
 * ${base}表
 *
 * @author 郭满亮
 * @version 2.0.0
 * @since 2.0.0 2019-02-01 19:19:48
 */
@Service("teacherCourseTeachClassService")
public class TeacherCourseTeachClassServiceImpl extends BaseServicePlusImpl<TeacherCourseTeachClassDao, TeacherCourseTeachClassEntity> implements TeacherCourseTeachClassService {
	@Resource
    private RedisTemplate redisTemplate;
	//region 模板生成
    @Resource
    private TeacherCourseTeachClassDao teacherCourseTeachClassDao;
    @Resource
    private TeacherCourseTeachClassService teacherCourseTeachClassService;
    @Resource
    private TeacherTeachClassService teacherTeachClassService;
    @Resource
    private StudentTeachClassService studentTeachClassService;
    @Resource
    private TeachClassService teachClassService;
    @Resource
    private StudentDao studentDao;
    @Resource
    private TeacherTeachClassDao teacherTeachClassDao;
    //endregion

    /* **********************************以下为非模板生成的内容********************************* */
    /**
     * 面对面建组教师输入4位数字
     *
     * @param temporaryClassStudentModels   教师信息
     * @return 临时组成员
     * @author 郭满亮
     * @since 2.0.0 2019-02-01 19:19:48
     */
    @Override
    public ItooResult saveTeacher(TemporaryClassStudentModel temporaryClassStudentModels) {
        String keyText = temporaryClassStudentModels.getKeyText();
        if (keyText == null) {
            return ItooResult.build(ItooResult.FAIL, "未获得keyText值");
        }
        // 根据key值查询返回
        TemporaryClassStudentModel temporaryClassStudentModel = (TemporaryClassStudentModel) redisTemplate.opsForValue().get(keyText);

        if (temporaryClassStudentModel == null) {
            temporaryClassStudentModel = new TemporaryClassStudentModel();
            // 上课班ID
            String teachClassId = IdWorker.getIdStr();
            temporaryClassStudentModel.setName(temporaryClassStudentModels.getName());
            temporaryClassStudentModel.setId(temporaryClassStudentModels.getId());
            temporaryClassStudentModel.setKeyText(keyText);
            temporaryClassStudentModel.setTeachClassId(teachClassId);
            temporaryClassStudentModel.setCourseId(temporaryClassStudentModels.getCourseId());
            temporaryClassStudentModel.setCourseName(temporaryClassStudentModels.getCourseName());
            redisTemplate.opsForValue().set(keyText,temporaryClassStudentModel);
            redisTemplate.expire(keyText, 2400, TimeUnit.SECONDS);
            return ItooResult.build(ItooResult.SUCCESS, "添加成功",temporaryClassStudentModel);
        } else {
            temporaryClassStudentModel.setCourseId(temporaryClassStudentModels.getCourseId());
            temporaryClassStudentModel.setCourseName(temporaryClassStudentModels.getCourseName());
            redisTemplate.opsForValue().set(keyText,temporaryClassStudentModel);
            redisTemplate.expire(keyText, 2400, TimeUnit.SECONDS);
            return ItooResult.build(ItooResult.SUCCESS, "查询成功",temporaryClassStudentModel);
        }
    }

    /**
     * 面对面建组将输入同样key值的学生统计出来
     *
     * @param temporaryClassStudentModels   学生信息
     * @return 临时组成员
     * @author 郭满亮
     * @since 2.0.0 2019-02-01 19:19:48
     */
    @Override
    public ItooResult saveStudent(TemporaryClassStudentModel temporaryClassStudentModels) {
        String keyText = temporaryClassStudentModels.getKeyText();
        if (keyText == null) {
            return ItooResult.build(ItooResult.FAIL, "未获得keyText值");
        }
        // 根据key值查询返回
        TemporaryClassStudentModel temporaryClassStudentModel = (TemporaryClassStudentModel) redisTemplate.opsForValue().get(keyText);
        if (temporaryClassStudentModel == null) {
            return ItooResult.build(ItooResult.FAIL, "暂无此班课");
        }
        TemporaryClassStudentModel studentClassModel = new TemporaryClassStudentModel();
        studentClassModel.setId(temporaryClassStudentModels.getId());
        studentClassModel.setName(temporaryClassStudentModels.getName());
        studentClassModel.setTeachClassId(temporaryClassStudentModel.getTeachClassId());

        Set<TemporaryClassStudentModel> userSet = temporaryClassStudentModel.getUserSet();
        if (userSet == null){
            userSet = new HashSet<>();
        }
        userSet.add(studentClassModel);
        temporaryClassStudentModel.setUserSet(userSet);
        redisTemplate.opsForValue().set(keyText, temporaryClassStudentModel);
        redisTemplate.expire(keyText, 2400, TimeUnit.SECONDS);
        return ItooResult.build(ItooResult.SUCCESS, "添加成功", temporaryClassStudentModel);
    }

    /**
     * 建立上课班,将老师、学生添加到上课班
     * @param temporaryClassStudentModels 所有学生和教师
     * @return 创建是否成功
     * @author 郭满亮
     * @since 2019年2月9日16:10:09
     */
    @Override
    public ItooResult saveTeachClass(TemporaryClassStudentModel temporaryClassStudentModels){
        String keyText = temporaryClassStudentModels.getKeyText();
        if (keyText == null) {
            return ItooResult.build(ItooResult.FAIL, "未获得keyText值");
        }

        try {
            // 根据key值查询返回
            TemporaryClassStudentModel temporaryClassStudentModel = (TemporaryClassStudentModel) redisTemplate.opsForValue().get(keyText);
            redisTemplate.opsForValue().set(keyText,temporaryClassStudentModel);
            redisTemplate.expire(keyText, 1, TimeUnit.SECONDS);

            TeacherCourseTeachClassEntity teacherCourseTeachClassEntity = new TeacherCourseTeachClassEntity();
            TeacherTeachClassEntity teacherTeachClassEntity = new TeacherTeachClassEntity();
            List<StudentTeachClassEntity> studentTeachClassEntityList= new ArrayList<>();
            TeachClassEntity teachClassEntity = new TeachClassEntity();

            String courseName = temporaryClassStudentModel.getCourseName();
            String courseId = temporaryClassStudentModel.getCourseId();
            //从工具类中获取学年学期数据
            String semester = YearSemester.getYearSemesterCode(0, 0).get(0);
            // 教师-课程-上课班对应表
            teacherCourseTeachClassEntity.setCourseId(courseId);
            teacherCourseTeachClassEntity.setCourseName(courseName);
            teacherCourseTeachClassEntity.setSchoolYear(semester);
            teacherCourseTeachClassEntity.setTeachClassId(temporaryClassStudentModel.getTeachClassId());
            teacherCourseTeachClassEntity.setTeacherId(temporaryClassStudentModel.getId());
            teacherCourseTeachClassEntity.setOperator(temporaryClassStudentModel.getId());
            teacherCourseTeachClassEntity.setRemark("随堂小练");
            // 教师和上课班关系表
            teacherTeachClassEntity.setSchoolYear(semester);
            teacherTeachClassEntity.setTeachClassId(temporaryClassStudentModel.getTeachClassId());
            teacherTeachClassEntity.setTeacherId(temporaryClassStudentModel.getId());
            teacherTeachClassEntity.setOperator(temporaryClassStudentModel.getId());
            teacherTeachClassEntity.setRemark("随堂小练");
            // 上课班-学生管理表
            Set<TemporaryClassStudentModel> userSet = temporaryClassStudentModel.getUserSet();
            for (TemporaryClassStudentModel classStudentModel : userSet) {
                StudentTeachClassEntity studentTeachClassEntity = new StudentTeachClassEntity();
                studentTeachClassEntity.setStudentId(classStudentModel.getId());
                studentTeachClassEntity.setTeachClassId(classStudentModel.getTeachClassId());
                studentTeachClassEntity.setOperator(temporaryClassStudentModel.getId());
                studentTeachClassEntity.setRemark("随堂小练");
                studentTeachClassEntityList.add(studentTeachClassEntity);
            }

            // 班级表--关联教务2
            // 上课班代码
            String code = IdWorker.getIdStr();
            teachClassEntity.setCourseId(courseId);
            teachClassEntity.setCode(code);
            teachClassEntity.setSchoolYear(semester);
            teachClassEntity.setId(temporaryClassStudentModel.getTeachClassId());
            teachClassEntity.setName(temporaryClassStudentModels.getTeachClassName());
            teachClassEntity.setOperator(temporaryClassStudentModel.getId());
            teachClassEntity.setRemark("随堂小练");
            // 向数据库插入数据
            teacherCourseTeachClassService.save(teacherCourseTeachClassEntity) ;
            teacherTeachClassService.save(teacherTeachClassEntity) ;
            studentTeachClassService.saveBatch(studentTeachClassEntityList);
            teachClassService.save(teachClassEntity);

            return ItooResult.build(ItooResult.SUCCESS, "添加成功");
        }
        catch (Exception e) {
            e.printStackTrace();
            return ItooResult.build(ItooResult.FAIL, "添加失败");
        }

    }

    /**
     * 教师面对面添加学生
     *
     * @param studentCode   学生id
     * @param keyText 4位数字
     * @return 临时组所有成员
     * @author 郭满亮
     * @since 2.0.0 2019年2月24日19:07:33
     */
    @Override
    public ItooResult saveAddStudent(String studentCode, String keyText) {
        // 根据key值查询返回
        TemporaryClassStudentModel temporaryClassStudentModel = (TemporaryClassStudentModel) redisTemplate.opsForValue().get(keyText);
        // 根据studentCode查询学生信息
        StudentEntity studentEntity = studentDao.findByCode(studentCode);
        TemporaryClassStudentModel studentClassModel = new TemporaryClassStudentModel();
        studentClassModel.setId(studentEntity.getId());
        studentClassModel.setName(studentEntity.getName());
        studentClassModel.setTeachClassId(temporaryClassStudentModel.getTeachClassId());

        Set<TemporaryClassStudentModel> userSet = temporaryClassStudentModel.getUserSet();
        if (userSet == null){
            userSet = new HashSet<>();
        }
        userSet.add(studentClassModel);
        temporaryClassStudentModel.setUserSet(userSet);
        redisTemplate.opsForValue().set(keyText, temporaryClassStudentModel);
        redisTemplate.expire(keyText, 2400, TimeUnit.SECONDS);
        return ItooResult.build(ItooResult.SUCCESS, "添加成功", temporaryClassStudentModel);
    }

    /**
     * 教师面对面移除学生
     *
     * @param studentSet   全部教师信息
     * @return 移除结果
     * @author 郭满亮
     * @since 2.0.0 2019年2月24日19:07:33
     */
    @Override
    public ItooResult saveRemoveStudent(TemporaryClassStudentModel studentSet) {
        // 根据key值查询返回
        redisTemplate.opsForValue().set(studentSet.getKeyText(), studentSet);
        redisTemplate.expire(studentSet.getKeyText(), 2400, TimeUnit.SECONDS);
        return ItooResult.build(ItooResult.SUCCESS, "移除成功");
    }

    /**
     * 根据教师id和课程id查询上课班
     *
     * @param teacherId 教师id
     * @param courseId 课程id
     * @return  上课班
     * @author 郭满亮
     * @since 2019年2月27日16:04:58
     */
    @Override
    public List<SelectTeacherTeachClassModel> getTeachClass(String teacherId, String courseId) {
        //从工具类中获取学年学期数据
        String semester = YearSemester.getYearSemesterCode(0, 0).get(0);
        TeacherCourseTeachClassEntity teacherCourse=new TeacherCourseTeachClassEntity();
        teacherCourse.setSchoolYear(semester);
        teacherCourse.setTeacherId(teacherId);
        teacherCourse.setCourseId(courseId);
        // 教師所有班級
        List<SelectTeacherTeachClassModel> teacherTeachClassEntities = teacherTeachClassDao.getByTeacherId(teacherCourse);
        return teacherTeachClassEntities;
    }

    /**
     * 根据教师ID查询出课程
     * 王华伟--2019年5月4日09:44:00
     * @param param
     * @return
     */
    @Override
    public List<TeacherCourseTeachClassModel> selectStudentyByTid(Map param){
        List<TeacherCourseTeachClassModel> teacherTeachClassEntities = teacherCourseTeachClassDao.selectStudentyByTid(param);
        return teacherTeachClassEntities;
    }

}
